﻿using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;

namespace Heyo.Class.Helper.Encryption
{
    public class Sha1Helper
    {
        /// <summary>
        /// 将字符串进行SHA1加密，返回8位无符号数组
        /// </summary>
        /// <param name="str"></param>
        /// <returns>返回8位无符号数组</returns>
        public static byte[] EncryptToSha1(string str)
        {
            SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
            byte[] str1 = System.Text.Encoding.UTF8.GetBytes(str);
            byte[] str2 = sha1.ComputeHash(str1);
            sha1.Clear();
            ((IDisposable) sha1).Dispose();
            return str2;
        }
        /// <summary>
        /// 将字符串进行SHA1加密，返回字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns>返回字符串</returns>
        public static string EncryptToSha1String(string str)
        {
            byte[] bytHash = EncryptToSha1(str);
            string sTemp = bytHash.Aggregate("", (current, t) => current + t.ToString("X").PadLeft(2, '0'));
            return sTemp.ToLower();
        }

        public static string GetDataSha1(Stream stream)
        {
            HashAlgorithm algorithm= SHA1.Create();
            byte[] buf = algorithm.ComputeHash(stream);
            return BitConverter.ToString(buf).Replace("-", "");
        }
    }
}